<?php


/**
 * Notes:
 * PROJECT_NAME : ERP v1.8 重构erp
 * User: 夏鱼儿<397072174@qq.com>
 * Date: 2024/5/18
 * Time: 15:09
 */

namespace app\command\company;

use app\company\model\Store;
use app\tms\model\StoreOrder;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use app\company\model\PickingStore as PickingStoreModel;
use think\facade\Log;

/**
 *  Class PickingCompany
 *  公司分拣单
 */
class PickingStore extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('picking:store')->setDescription('门店分拣单')
             ->addArgument('date', Option::VALUE_REQUIRED, '日期', '');
    }


    protected function execute(Input $input, Output $output)
    {
        $date = $input->getArgument('date');
        if (!$date) $date = date('Ymd');
        $date  = date('Y-m-d', strtotime($date));
        $model = new Store();
        try {
            $arr = $model->column('id store_id,name,company_id');
            foreach ($arr as $k => $v) {

                $arr[$k]['pick_time'] = strtotime($date);
                $arr[$k]['name']      = $v['name'] . "PS" . $date;
                $a                    = date('Y-m-d', strtotime("-1 day", strtotime($date)));
                if (!StoreOrder::storeId($v['store_id'])->whereDay("create_time", $a)->whereIn('order_status', [2, 3, 4])->count()) unset($arr[$k]);
            }
            (new PickingStoreModel())->saveAll($arr);
            $output->writeln($date . '门店分拣单生成成功');
        } catch (\Exception $e) {
            $output->writeln($e->getMessage());
        }
    }
}